library(ggplot2)
library(tidyr)


# Figure 5: Preferred Nuclear Theory (Self-Reported)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_2/")

Dat <- read.csv("Paper_2_Data_Clean.csv", stringsAsFactors = FALSE)

Dat <-
  Dat %>%
  mutate(NuclearTheory_fac = factor(
    NuclearTheory,
    levels = 1:4,
    labels = c(
      "Never-Evers",
      "Existential\nDeterrence",
      "Offense/\nDefense",
      "Utilitarian"
    )
  )) %>% 
  filter(!is.na(NuclearTheory_fac))

gg_df <-
  Dat %>% 
  group_by(Nuclear, NuclearTheory) %>% 
  summarize(tidy(lm_robust(NuclearTheory ~ 1, data = cur_data())))

p <-
  ggplot(Dat, aes(x = NuclearTheory_fac, fill = as.factor(Nuclear))) +
  geom_bar() +
  # geom_text(data = label_df, aes(y = y, label = label, fill = NULL)) +
  ylab("Count") + xlab("Preferred Nuclear Theory") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5)) +
  ggtitle("") +
  theme_bw() +
  scale_fill_manual(
    breaks = levels(factor(0:1)),
    values = c("0" = "cornflowerblue", "1" = "coral1"),
    labels = c("No", "Yes"),
    name = "Supported Nuclear Use\n(At Any Threshold)"
  )



g <- p + annotate("text", x=1, y=10, label="62.5%") + 
  annotate("text", x=2, y=10, label="87.6%") + 
  annotate("text", x=3, y=10, label="91.5%") + 
  annotate("text", x=4, y=10, label="91.8%")+
  annotate("text", x=1, y=460, label="37.5%") + 
  annotate("text", x=2, y=206, label="12.4%") + 
  annotate("text", x=3, y=258, label="8.5%") + 
  annotate("text", x=4, y=158, label="8.2%")

g


ggsave("Image_5.png", plot=g, width=2000, height=2000, units=c("px"))



# 
# c(table(Dat$NuclearTheory))
# 
# 168/448
# 280/448
# 
# 24/(24+170)
# 170/(24+170)
#   
# 21/(21+225)
# 225/(21+225)
#     
# 12/(12+134)
# 134/(12+134)